﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Xml;

#endregion

namespace ScrumTable.Common.Xml
{
    /// <summary>
    /// Contains the global utilities for the XmlNode class.
    /// </summary>
    public static class XmlNodeUtil
    {
        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the attrbiute value of the given xml attribute.
        /// </summary>
        /// <param name="node">Specifies the parent xml node instance.</param>
        /// <param name="attributeName">Specifies the name of the node.</param>
        /// <returns>Returns the retrieved value.</returns>
        public static T GetAttributeValue<T>(this XmlNode node, string attributeName)
        {
            PreCondition.AssertNotNull(node, "node");
            PreCondition.AssertNotNullOrEmpty(attributeName, "attributeName");

            if (node.Attributes != null && node.Attributes[attributeName] != null)
            {
                string attribValue = node.Attributes[attributeName].Value;

                if (attribValue != null)
                {
                    return attribValue.ChangeType<T>();
                }
            }
            return default(T);
        }

        /// <summary>
        /// Gets the attrbiute value of the given xml attribute.
        /// </summary>
        /// <param name="node">Specifies the parent xml node instance.</param>
        /// <param name="attributeName">Specifies the name of the node.</param>
        /// <returns>Returns the retrieved value.</returns>
        public static string GetAttributeValue(this XmlNode node, string attributeName)
        {
            return GetAttributeValue<string>(node, attributeName);
        }

        #endregion
    }
}
